import 'package:fate_blind_box/utils/loading.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../../../theme/theme_color.dart';
import '../../../utils/common_widget.dart';
import '../../../network/http_request.dart';

class ShopPhoneEditPage extends StatelessWidget {
  Map<String, dynamic>? resMap;
  Function? refreshBlock;
  bool isFromUpload = false;
  Function? callBlock;

  ShopPhoneEditPage(
      {Key? key,
      this.resMap,
      this.refreshBlock,
      this.isFromUpload = false,
      this.callBlock})
      : super(key: key);
  TextEditingController phoneController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    phoneController.text =
        (resMap?['mobilePhone'] == null || resMap?['mobilePhone'] == '')
            ? ''
            : resMap?['mobilePhone'];
    return AppWidget.navTitleBar(
      bgColor: const Color(0xFFF6F6F6),
      title: '修改商家电话',
      rightActions: [
        UnconstrainedBox(
          child: CupertinoButton(
            padding: EdgeInsets.zero,
            onPressed: () {
              if (phoneController.text.isEmpty) {
                toast('请输入商家电话');
                return;
              }
              if (isFromUpload) {
                if(callBlock!=null){
                  callBlock!(phoneController.text);
                }
                Get.back();
              } else {
                subMitAddressInfo();
              }
            },
            child: Container(
              margin: const EdgeInsets.only(right: 15),
              width: 54,
              height: 31,
              decoration: BoxDecoration(
                color: ThemeColor.appColor,
                borderRadius: BorderRadius.circular(5),
              ),
              alignment: Alignment.center,
              child: const Text(
                '完成',
                style: TextStyle(color: Colors.white, fontSize: 16),
              ),
            ),
          ),
        ),
      ],
      childWidget: Container(
        margin: const EdgeInsets.only(left: 15, right: 15, top: 55),
        padding: const EdgeInsets.only(top: 15, bottom: 15, left: 5, right: 5),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(4),
        ),
        child: TextField(
          inputFormatters: [FilteringTextInputFormatter.digitsOnly],
          maxLines: 1,
          controller: phoneController,
          obscureText: false,
          keyboardType: TextInputType.number,
          cursorColor: ThemeColor.sendMsgTextColor,
          style: TextStyle(fontSize: 16, color: ThemeColor.textColor),
          decoration: const InputDecoration(
            hintText: "请输入商家电话",
            isCollapsed: true,
            border: InputBorder.none,
          ),
        ),
      ),
    );
  }

  subMitAddressInfo() async {
    String phoneStr = phoneController.text;
    if (phoneStr.length > 11) {
      toast('请输入正确的手机号');
      return;
    }
    Map<String, dynamic> datMap = {
      "phone": phoneController.text,
    };
    var res = await HttpRequest.request('match-maker/business/uploadPhone',
        params: datMap);
    if (res['code'] == 0) {
      final data = res['data'];
      if (data != null) {
        toast('保存成功');
        if (refreshBlock != null) {
          refreshBlock!();
        }
        Get.back();
      } else {
        toast(res['msg']);
      }
    }
  }
}
